<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    // --------------------- 转换成字符串类型 ---------------------
    console.log('--------- 转换成字符串类型 ---------')
    let a = 10;

    // 方式一：拼接空串 
    let str1 = a + '';
    console.log(str1);
    console.log(typeof str1); // string 

    // 方式二：String(a) 
    let str2 = String(a);
    console.log(str2);
    console.log(typeof str2); // string 

    // 方式三：toString() 
    let str3 = a.toString();
    console.log(str3);
    console.log(typeof str3); // string 


    // --------------------- 转换成数值类型 ---------------------
    console.log('--------- 转换成数值类型 ---------')
    let s1 = '123abc';
    let s2 = 'aaa222';
    let s3 = '333';

    console.log('Number s1 = ' + Number(s1)); // NaN 
    console.log('Number s2 = ' + Number(s2)); // NaN 
    console.log('Number s3 = ' + Number(s3)); // 333 

    console.log('parseInt s1 = ' + parseInt(s1)); // 123 
    console.log('parseInt s2 = ' + parseInt(s2)); // NaN 
    console.log('parseInt s3 = ' + parseInt(s3)); // 333 

    let s4 = '12.3.45';
    console.log('parseFloat s4 = ' + parseFloat(s4)); // 12.3 

    let str = '300';
    console.log(+str); // 取正 300 
    console.log(-str); // 去负 -300 
    console.log(str + 10); // 直接拼接 30010 
    console.log(str - 10); // 先转成数值，再进行运算 290 
    console.log(str * 10); // 先转成数值，再进行运算 3000 


    // --------------------- 转换成布尔类型 ---------------------
    console.log('--------- 转换成布尔类型 ---------')
    let o1; // undefined 
    console.log(Boolean(o1)); // false 

    let o2 = ''; // 空串 
    console.log(Boolean(o2)); // false 

    let o3 = null; // 空对象 
    console.log(Boolean(o3)); // false 

    let o4 = 0;
    console.log(Boolean(o4)); // false 

    let o22 = ' '; 
    console.log(Boolean(o22)); // true 

    console.log(Boolean(parseInt(s2))); // Boolean(NaN) --> false 
</script>
</html>